package com.example.backend.util;

import javax.servlet.http.HttpServletRequest;

public class IpUtils {
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            String[] parts = ip.split(",");
            for (String part : parts) {
                part = part.trim();
                if (part.length() != 0 && !"unknown".equalsIgnoreCase(part)) {
                    return part;
                }
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
        return request.getRemoteAddr();
    }
}
